<template>
   <div>
      <v-input :detailData="inputData" :inputConfig="inputConfig" :rules="rules" @keyEnter="keyEnter" @keyBlue="keyBlue"
               ref="input"></v-input>
   </div>
</template>

<script>
import input from "@/components/commons/Detail";

export default {
   name: "",
   data() {
      return {
         cartontypeFlag: true,
         weightFlag: true,
         inputData: {
            shippingkey: "",
            carriercode: "",
            palletkey: "",
            cartontype: "",
            ck_cartontype: true,
            expresskey: "",
            weight: "",
            ck_weight: true,
            diffweight: "",
            maxBoxNumber: 100,
            diffrate: "",
            totalBoxNumber: "0"
         },
         rules: {},
      }
   },
   computed: {
      inputConfig() {
         return [
            [
               {
                  type: "input",
                  label: this.$t('out.shippingkey'),
                  props: "shippingkey",
                  disabled: true
               },
               {
                  type: "select",
                  label: this.$t('config.carrierCode'),
                  props: "carriercode",
                  options: [],
               },
               {
                  type: "input",
                  label: this.$t('out.palletkey'),
                  props: "palletkey",
               },
               {
                  type: "input",
                  label: this.$t('out.cartontype'),
                  props: "cartontype",
                  checkbox: true,
                  checkboxProps: "ck_cartontype",
                  disabled: this.cartontypeFlag
               },
               {
                  type: "input",
                  label: this.$t('out.expresskey'),
                  props: "expresskey",
               },
            ],
            [
               {
                  type: "input",
                  label: this.$t('out.ck_weight'),
                  props: "weight",
                  checkbox: true,
                  checkboxProps: "ck_weight",
                  disabled: this.weightFlag
               },
               {
                  type: "input",
                  label: this.$t('out.diffweight'),
                  props: "diffweight",
                  disabled: true
               },
               {
                  type: "input",
                  label: this.$t('out.maxBoxNumber'),
                  props: "maxBoxNumber",
               },
               {
                  type: "input",
                  label: this.$t('out.diffrate'),
                  props: "diffrate",
                  disabled: true
               },
               {
                  type: "input",
                  label: this.$t('out.totalBoxNumber'),
                  props: "totalBoxNumber",
                  disabled: true
               },
            ],
         ]
      }
   },
   methods: {
      keyEnter(name, data) {
         if (name === "weight") {
            let weight = this.inputData.weight.toUpperCase();
            let flag = this.checkWeight(weight);
            if (flag) {
               this.inputData.expresskey = this.inputData.expresskey.toUpperCase();
               this.$refs.input.inputFocus("expresskey")
            }
         } else if (name === "palletkey") {
            if (this.inputData.carriercode === "") {
               this.errorPlay();
               this.$message.error(this.$selectPlease("config.carrierCode"));
               return;
            }
            let flag = this.checkPalletkeyIsNull()
            if (!flag) {
               return;
            }
            this.checkPalletkey(this.inputData.palletkey);
            if (this.inputData.ck_cartontype) {
               this.$refs.input.inputFocus("expresskey");
            } else {
               this.$refs.input.inputFocus("cartontype");
            }
         } else if (name === "cartontype") {
            let flag = this.checkCartontypeIsNull();
            if (!flag) {
               return;
            }
            this.checkCartontype(data);
         } else if (name === "maxBoxNumber") {
            this.maxBoxNumber()
         } else if (name === "expresskey") {
            let flag = this.checkExpresskeyIsNull()
            if (!flag) {
               return;
            }
            let cartontype = this.inputData.cartontype.toUpperCase();
            if (cartontype === "") {
               this.errorPlay();
               this.$message.error(this.$tj('out.cartontype','app.emptyMes'));
               return;
            }
            let maxBoxNumber = this.validateMaxBoxNumber();
            if (!maxBoxNumber) {
               return;
            }
            let totalBoxNumber = this.inputData.totalBoxNumber;
            if (parseInt(totalBoxNumber) > parseInt(this.inputData.maxBoxNumber)) {
               this.$message.error(this.$t('msg.msg57'));
               this.errorPlay();
               return;
            }
            this.checkExpresskey(this.inputData.expresskey)
         }
      },
      keyBlue(name, data) {
         if (name === "maxBoxNumber") {
            this.maxBoxNumber()
         }
      },
      async getWeightByDropid(expresskey) {
         try {
            let res = await this.$api.post(`/shipkd/getWeightByDropid`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               opeuser: this.$parent.opeuser,
               expresskey: expresskey
            })
            if (res.data.type === "success") {
               this.inputData.weight = res.data.dropidWeight;
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.errorPlay();
         }
      },
      checkWeight(data) {
         if (data === "") {
            this.errorPlay();
            this.$message.error(this.$tj('out.weight','app.emptyMes'));
            this.inputData.weight = "";
            this.$refs.input.inputFocus("weight");
            return false;
         }
         if (data == "0" || data == "0.0" || data == "0.00" || data == "0.000") {
            this.errorPlay();
            this.$message.error(this.$t('msg.msg58'));
            this.inputData.weight = "";
            this.$refs.input.inputFocus("weight");
            return false;
         }
         let reg = /^[1-9]\d*$/;
         let reg1 = /^[0][.]\d{1,}$/;
         let reg2 = /^[1-9]{1,}[.]\d{1,}$/;
         let reg4 = /^(([^0][0-9]+|0)\.([0-9]{1,4}))$/;
         if (reg.test(data) === true || reg1.test(data) === true || reg2.test(data) === true || reg4.test(data) === true) {
            return true;
         } else {
            this.errorPlay();
            this.$message.error(this.$t('msg.msg58'));
            this.inputData.weight = "";
            this.$refs.input.inputFocus("weight");
            return false;
         }
      },
      checkPalletkeyIsNull() {
         this.inputData.palletkey = this.inputData.palletkey.toUpperCase();
         if (this.inputData.palletkey === "") {
            this.errorPlay();
            this.$refs.input.inputFocus("palletkey");
            this.$message.error(this.$tj('out.palletkey','app.emptyMes'));
            return false;
         }
         return true;
      },
      async checkPalletkey(palletkey) {
         try {
            let res = await this.$api.post(`/shipkd/palletScan`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               opeuser: this.$parent.opeuser,
               shippingkey: this.inputData.shippingkey,
               palletkey: palletkey
            })
            if (res.data.type === "error") {
               this.errorPlay();
               this.inputData.palletkey = "";
               this.$refs.input.inputFocus("palletkey");
               this.$message.error(res.data.msg);
            } else if (res.data.type === "success") {
               this.okPlay();
            }
         } catch (e) {
            this.errorPlay();
            this.$message.error(this.$t('app.err'));
         }
      },
      checkCartontypeIsNull() {
         this.checkPalletkeyIsNull();
         this.inputData.cartontype = this.inputData.cartontype.toUpperCase();
         if (this.inputData.cartontype === "") {
            this.errorPlay();
            this.$refs.input.inputFocus("cartontype");
            this.$message.error(this.$tj('out.cartontype','app.emptyMes'));
            return false;
         }
         return true;
      },
      async checkCartontype(cartontype) {
         try {
            let res = await this.$api.post(`/shipkd/cartontypeScan`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               opeuser: this.$parent.opeuser,
               cartontype: cartontype
            })
            if (res.data.type === "error") {
               this.errorPlay();
               this.inputData.cartontype = "";
               this.$refs.input.inputFocus("cartontype");
               this.$message.error(res.data.msg);
            } else if (res.data.type === "success") {
               this.okPlay();
               this.$refs.input.inputFocus("expresskey");
            }
         } catch (e) {
            this.errorPlay();
            this.$message.error(this.$t('app.err'));
         }
      },
      maxBoxNumber() {
         let flag = this.validateMaxBoxNumber()
         if (!flag) {
            return;
         }
         this.$refs.input.inputFocus("expresskey");
      },
      validateMaxBoxNumber() {
         let qty = this.inputData.maxBoxNumber;
         if (qty === "") {
            this.errorPlay();
            this.$message.error(this.$tj('out.maxBoxNumber'));
            this.$refs.input.inputFocus("maxBoxNumber");
            return false;
         }
         let reg = /^[1-9]\d*$/;
         if (reg.test(qty) === false) {
            this.errorPlay();
            this.$message.error(qty + "集货最大箱数格式不对，必须为正整数");
            this.inputData.maxBoxNumber = "";
            this.$refs.input.inputFocus("maxBoxNumber");
            return false;
         }
         return true;
      },
      checkExpresskeyIsNull() {
         this.checkPalletkeyIsNull();
         this.checkCartontypeIsNull();
         let expresskey = this.inputData.expresskey.toUpperCase();
         if (expresskey === "") {
            this.errorPlay();
            this.$refs.input.inputFocus("expresskey");
            this.$message.error(this.$tj('out.expresskey','app.emptyMes'));
            return false;
         }
         let weight = this.inputData.weight.toUpperCase();
         let flag = this.checkWeight(weight);
         if (!flag) {
            return false;
         }
         return true;
      },
      async checkExpresskey(expresskey) {
         let cartontype = this.inputData.cartontype.toUpperCase();
         let carriercode = this.inputData.carriercode;
         let weight = this.inputData.weight;
         try {
            let res = await this.$api.post(`/shipkd/expresskeyScan`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               opeuser: this.$parent.opeuser,
               shippingkey: this.inputData.shippingkey,
               expresskey: expresskey,
               weight: weight,
               carriercode: carriercode,
               cartontype: cartontype
            })
            if (res.data.type === "error") {
               this.errorPlay();
               this.inputData.expresskey = "";
               this.$refs.input.inputFocus("expresskey");
               this.$message.error(res.data.msg);
            } else if (res.data.type === "alertMsg") {
               this.inputData.diffweight = res.data.diffweight;
               this.inputData.diffrate = res.data.diffrate;
               this.$confirm('差异率超出要求，是否确认通过', '', {
                  confirmButtonText: this.$t('common.confirm'),
                  cancelButtonText: this.$t('common.cancel'),
               }).then(() => {
                  this.dealAction()
               }).catch(() => {
                  this.inputData.expresskey = "";
                  this.$refs.input.inputFocus("expresskey");
               });
            } else if (res.data.type === "success") {
               this.okPlay();
               await this.dealAction()
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.errorPlay();
         }
      },
      async dealAction() {
         let carriercode = this.inputData.carriercode;
         if (carriercode === "") {
            this.errorPlay();
            this.$message.error(this.$selectPlease("config.carrierCode"));
            return;
         }
         let weight = this.inputData.weight;
         this.checkPalletkeyIsNull();
         this.checkCartontypeIsNull();
         this.checkExpresskeyIsNull();
         this.checkWeight(weight);
         let expresskey = this.inputData.expresskey.toUpperCase();
         let cartontype = this.inputData.cartontype.toUpperCase();
         try {
            let res = await this.$api.post(`/shipkd/dealAction`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               opeuser: this.$parent.opeuser,
               shippingkey: this.inputData.shippingkey,
               palletkey: this.inputData.palletkey,
               carriercode: carriercode,
               expresskey: expresskey,
               weight: weight,
               cartontype: cartontype,
               maxBoxNumber: this.inputData.maxBoxNumber,
               totalBoxNumber: this.inputData.totalBoxNumber
            });
            if (res.data.type === "error") {
               this.errorPlay();
               this.inputData.expresskey = "";
               this.$refs.input.inputFocus("expresskey");
               this.$message.error(res.data.msg);
            } else if (res.data.type === "success") {
               this.okPlay();
               if (res.data.finishFlag === "NEW") {
                  this.inputData.shippingkey = res.data.newShippingkey;
                  this.$refs.input.inputFocus("palletkey");
                  this.inputData.expresskey = "";
                  this.inputData.totalBoxNumber = 0;
                  this.inputData.maxBoxNumber = 100;
                  this.inputData.weight = "";
                  this.inputData.cartontype = "";
                  this.inputData.diffrate = "";
                  this.inputData.diffweight = "";
                  this.$parent.$refs.table.tableData = [];
                  this.$message.success(this.$t('msg.msg78'));
                  this.openErrorMessageForm("palletkey", this.$t('msg.msg78'));
               } else {
                  this.inputData.expresskey = "";
                  this.$refs.input.inputFocus("expresskey");
                  await this.loadShipKDDetail()
               }
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.errorPlay();
         }
      },
      openErrorMessageForm(objID, errMessage) {
         let name = objID + "Ref";
         this.$refs.input.$refs[name][0].blur();
         this.$confirm(errMessage, '提示', {
            confirmButtonText: this.$t('common.confirm'),
            cancelButtonText: this.$t('common.cancel'),
         }).then(() => {
            this.inputData[objID] = "";
            this.$refs.input.inputFocus(objID);
         }).catch(() => {

         });
      },
      async loadShipKDDetail() {
         try {
            let res = await this.$api.post(`/loadShipKDDetail`, {
               whseid: this.$parent.whseid,
               users: this.$parent.users,
               shippingkey: this.inputData.shippingkey
            })
            if (res.data.type === "error") {
               this.errorPlay();
               this.$message.error(res.data.msg);
            } else {
               this.$parent.$refs.table.tableData = res.data.totalBoxNumber;
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.errorPlay();
         }
      }
   },
   mounted() {
      this.$refs.input.inputFocus("palletkey");
   },
   watch: {
      "inputData.ck_cartontype": {
         handler(data) {
            if (data === true) {
               this.inputData.cartontype = ""
               this.cartontypeFlag = true;
            } else {
               this.cartontypeFlag = false;
            }
         },
         immediate: true,
         deep: true
      },
      "inputData.ck_weight": {
         handler(data) {
            if (data === true) {
               this.inputData.weight = ""
               this.weightFlag = true;
            } else {
               this.weightFlag = false;
               let expresskey = this.$trim(this.inputData.expresskey).toUpperCase();
               if (expresskey === "") {
                  this.$refs.input.inputFocus("weight");
               } else {
                  this.getWeightByDropid(expresskey);
               }
            }
         },
         immediate: true,
         deep: true
      }
   },
   components: {
      "v-input": input,
   }
}
</script>

<style scoped>

</style>
